const { exec } = require("../db/mysql");
const { genPassword } = require("../utils/crypto");
const xss = require("xss");
// 校验登录
const loginCheck = (id) => {
  const sql = `select user_email from user where user_id='${id}'`;
  return exec(sql).then((rows) => {
    return rows[0] || {};
  });
};
const authCheck = (id) => {
  const sql = `select isadmin from user where user_id='${id}'`;
  return exec(sql).then((rows) => {
    return rows[0] || {};
  });
};
const aLogin = (a_name, a_pwd) => {
  a_name = escape(a_name);
  a_pwd = escape(a_pwd);

  const sql = `select * from admin where a_name='${a_name}' and a_pwd='${a_pwd}'`;
  return exec(sql).then((rows) => {
    return rows[0] || {};
  });
};
const tLogin = (tch_num, tch_pwd) => {
  tch_num = escape(tch_num);
  tch_pwd = escape(tch_pwd);

  const sql = `select * from teacher where tch_num='${tch_num}' and tch_pwd='${tch_pwd}'`;
  return exec(sql).then((rows) => {
    return rows[0] || {};
  });
};
const sLogin = (stu_num, stu_pwd) => {
  stu_num = escape(stu_num);
  stu_pwd = escape(stu_pwd);

  const sql = `select * from student where stu_num='${stu_num}' and stu_pwd='${stu_pwd}'`;
  console.log(sql);
  return exec(sql).then((rows) => {
    return rows[0] || {};
  });
};
//******************************************************** */
// 用户注册模块
const addStu = (userData) => {
  let data = userData.data;
  let arr = [];
  for (let i = 0; i < data.length; i++) {
    let temp = [];
    temp = temp.concat(data[i].stu_name);
    temp = temp.concat(data[i].stu_pwd);
    temp = temp.concat(data[i].stu_num);
    temp = temp.concat(data[i].stu_class);
    temp = temp.concat(data[i].stu_phone);
    arr = arr.concat([temp]);
  }
  const sql = `insert into student(stu_name, stu_pwd, stu_num, stu_class,stu_phone) values ?`;

  return exec(sql, arr).then((insertData) => {
    if (insertData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const getStuList = () => {
  const sql = `select * from student`;
  return exec(sql);
};
const delStu = (id) => {
  const Selsql = `select sel_exp.stu_id from sel_exp where sel_exp.stu_id=${id}`;
  return exec(Selsql).then((rows) => {
    if (rows[0] === undefined) {
      const sql = `delete from student where stu_id=${id}`;
      return exec(sql).then((delData) => {
        if (delData.affectedRows > 0) {
          return true;
        }
        return false;
      });
    } else {
      return {
        code: -1,
        msg: "无法删除该用户",
      };
    }
  });
};
const delSomeStu = (userData = {}) => {
  let arr = userData.arr;
  let len = arr.length;
  let sql = `delete from student where stu_id in (`;
  for (let i = 0; i < len - 1; i++) {
    sql += arr[i] + `,`;
  }
  sql += arr[len - 1] + `)`;

  return exec(sql).then((delData) => {
    if (delData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const updateStu = (id, userData = {}) => {
  let stu_name = userData.stu_name;
  let stu_pwd = userData.stu_pwd;
  let stu_num = userData.stu_num;
  let stu_class = userData.stu_class;
  let stu_phone = userData.stu_phone;
  const sql = `update student set stu_name='${stu_name}',stu_pwd='${stu_pwd}', 
  stu_num='${stu_num}',stu_class='${stu_class}',stu_phone = '${stu_phone}' where stu_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};

const sleSingleStu = (id) => {
  const sql = `select * from student where stu_num='${id}'`;
  return exec(sql)
};
//*************教师****************** */
const addTch = (userData) => {
  let data = userData.data;
  let arr = [];
  for (let i = 0; i < data.length; i++) {
    let temp = [];
    temp = temp.concat(data[i].tch_name);
    temp = temp.concat(data[i].tch_pwd);
    temp = temp.concat(data[i].tch_num);
    temp = temp.concat(data[i].tch_title);
    temp = temp.concat(data[i].tch_phone);
    arr = arr.concat([temp]);
  }
  const sql = `insert into teacher(tch_name, tch_pwd, tch_num, tch_title,tch_phone) values ?`;

  return exec(sql, arr).then((insertData) => {
    if (insertData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const getTchList = () => {
  const sql = `select * from teacher`;
  return exec(sql);
};
const delTch = (id) => {
  const Selsql = `select exp.tch_id from exp where exp.tch_id=${id}`;
  return exec(Selsql).then((rows) => {
    if (rows[0] === undefined) {
      const sql = `delete from teacher where tch_id=${id}`;
      return exec(sql).then((delData) => {
        if (delData.affectedRows > 0) {
          return true;
        }
        return false;
      });
    } else {
      return {
        code: -1,
        msg: "无法删除该用户",
      };
    }
  });
};
const delSomeTch = (userData = {}) => {
  let arr = userData.arr;
  let len = arr.length;
  let sql = `delete from teacher where tch_id in (`;
  for (let i = 0; i < len - 1; i++) {
    sql += arr[i] + `,`;
  }
  sql += arr[len - 1] + `)`;

  return exec(sql).then((delData) => {
    if (delData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const updateTch = (id, userData = {}) => {
  let tch_name = userData.tch_name;
  let tch_pwd = userData.tch_pwd;
  let tch_num = userData.tch_num;
  let tch_title = userData.tch_title;
  let tch_phone = userData.tch_phone;
  const sql = `update teacher set tch_name='${tch_name}',tch_pwd='${tch_pwd}', 
  tch_num='${tch_num}',tch_title='${tch_title}',tch_phone = '${tch_phone}' where tch_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const sleSingleTch = (id) => {
  const sql = `select * from teacher where tch_num='${id}'`;
  return exec(sql)
};
//---------------------------------------
const getStuNumber = () => {
  const sql = `select count(*) count from student`;
  return exec(sql);
};
const getTchNumber = () => {
  const sql = `select count(*) count from teacher`;
  return exec(sql);
};
const getExpNumber = () => {
  const sql = `select count(*) count from exp`;
  return exec(sql);
};

//+++++++++++++++++++++++++end++++++++++++++++++++++++++++++++++
const getPower = () => {
  const sql = `select * from power`;
  return exec(sql);
};
const UpdatePower = (id, userData = {}) => {
  let is_register = userData.is_register;
  const sql = `update power set is_register='${is_register}' where power_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
//************************************************************** */
const updateUserInfo = (id, userData = {}) => {
  let user_email = userData.user_email;
  let user_cname = xss(userData.user_cname);
  let user_code_id = userData.user_code_id;
  let user_gender = userData.user_gender;
  let user_git = xss(userData.user_git);
  let user_grade = userData.user_grade;
  let user_name = xss(userData.user_name);
  let user_study_dir = userData.user_study_dir;
  let user_type = userData.user_type;
  let user_work;
  if (userData.user_work) {
    user_work = userData.user_work;
  } else {
    user_work = "";
  }

  const sql = `update user set user_email='${user_email}',
  user_cname='${user_cname}', user_code_id='${user_code_id}', 
  user_gender='${user_gender}', user_git='${user_git}', 
  user_grade='${user_grade}', user_name='${user_name}', user_study_dir='${user_study_dir}', 
  user_type='${user_type}', user_work='${user_work}' where user_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const updateUserIntro = (id, userData = {}) => {
  let user_introduce = userData.user_introduce;
  const sql = `update user set user_introduce='${user_introduce}' where user_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
//**********老师***************/
const getTchIntro = () => {
  const sql = `select * from teacher`;
  return exec(sql);
};
const updateTchIntro = (userData = {}) => {
  let t_detail = userData.t_detail;
  // console.log(user_introduce);
  const sql = `update teacher set t_detail='${t_detail}'`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
//*****************学生状态******************* */
//获取用户状态
const getCodeList = () => {
  const sql = `select * from user_code`;
  return exec(sql);
};
const addCode = (userData = {}) => {
  const user_code_name = xss(userData.user_code_name);
  const sql = `insert into user_code (user_code_name) 
    values('${user_code_name}');`;

  return exec(sql).then((insertData) => {
    if (insertData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const updateCode = (id, userData = {}) => {
  const user_code_name = xss(userData.user_code_name);
  const sql = `update user_code set user_code_name='${user_code_name}' where user_code_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const delCode = (id) => {
  console.log(id);
  const Selsql = `select user.user_code_id from user where user_code_id=${id}`;
  return exec(Selsql).then((rows) => {
    if (rows[0] === undefined) {
      const sql = `delete from user_code where user_code_id=${id}`;
      return exec(sql).then((delData) => {
        if (delData.affectedRows > 0) {
          return true;
        }
        return false;
      });
    } else {
      return {
        code: -1,
        msg: "无法删除该类型",
      };
    }
  });
};
//**********************学生用户管理************************ */

// const getStuList = () => {
//   const sql = `select user.*,user_code.user_code_name from
//   user,user_code where user.user_code_id=user_code.user_code_id`;
//   return exec(sql);
// };

const updateAdmin = (id, userData = {}) => {
  let isadmin = userData.isadmin;
  const sql = `update user set isadmin='${isadmin}' where user_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
module.exports = {
  aLogin,
  sLogin,
  tLogin,
  addStu,
  getStuList,
  delStu,
  delSomeStu,
  updateStu,
  sleSingleStu,
  //老师
  addTch,
  getTchList,
  delTch,
  delSomeTch,
  updateTch,
  sleSingleTch,

  getStuNumber,
  getTchNumber,
  getExpNumber,
};
